/**
 * 
 */
package org.mspring.mlog.api.spider;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.List;

import org.jsoup.Connection;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.mspring.mlog.api.spider.crawler.DefaultCrawler;
import org.mspring.mlog.api.spider.entity.SpiderRule;
import org.mspring.mlog.api.spider.handler.impl.ContentImageHandler;
import org.mspring.mlog.api.spider.utils.ConnectionUtils;

/**
 * @author Gao Youbo
 * @since 2013-3-8
 * @description
 * @TODO
 */
public class SpiderTest {
    public static void main(String[] args) throws MalformedURLException, IOException {
        // new SpiderTest().test();
        new SpiderTest().testOSC();
    }

    public void testLUPA() throws IOException {
        Connection con = ConnectionUtils.getConnection("http://www.lupaworld.com/article-227521-1.html", 10000);
        con.header("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)");

        Document doc = con.get();
        SpiderRule rule = getLUPARule();

        Spider spider = new Spider(new DefaultCrawler());
        spider.addContentHandler("imageHandler", new ContentImageHandler());

        // List<String> urls = spider.getUrls(doc, rule);
        // for (String url : urls) {
        //
        // }

        Element title = spider.getTitle(doc, rule);
        System.out.println(title.text());
        Element content = spider.getContent(doc, rule);
        System.out.println(content.html());
    }

    public SpiderRule getLUPARule() {
        SpiderRule rule = new SpiderRule();
        rule.setListRule(".bm_c.xld dl.bbda dt.xs2");
        rule.setTitleRule(".mn div.bm.vw .h h1.ph.hm");
        rule.setContentRule(".mn div.bm.vw .d table td");
        return rule;
    }

    // public void test() throws MalformedURLException, IOException {
    // Connection con =
    // ConnectionUtils.getConnection("http://www.mspring.org/post/319.html",
    // 10000);
    // con.header("User-Agent",
    // "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)");
    // Document doc = con.get();
    // SpiderRule rule = getRule();
    //
    // Spider spilder = new Spider(new DefaultCrawler());
    // spilder.addContentHandler("imageHandler", new ContentImageHandler());
    //
    // Element content = spilder.getContent(doc, rule);
    // String a = content.html();
    // System.out.println(a);
    // }
    //
    // public SpiderRule getRule() {
    // SpiderRule rule = new SpiderRule();
    // rule.setTitleRule(".post-entity .post-title");
    // rule.setContentRule(".post-entity .post-content");
    // return rule;
    // }

    public void testOSC() throws IOException {
        Connection con = ConnectionUtils.getConnection("http://www.oschina.net/news/42560/libreoffice-4-1-is-closed", 10000);
        con.header("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)");

        Document doc = con.get();
        SpiderRule rule = getOSCRule();

        Spider spider = new Spider(new DefaultCrawler());
        spider.addContentHandler("imageHandler", new ContentImageHandler());

//        Element content = spider.getContent(doc, rule);
//        String a = content.html();
//        System.out.println(a);
        
        System.out.println(spider.getContent(doc, rule).text());

        // List<String> urls = spider.getUrls(doc, rule);
        // for (String url : urls) {
        // System.out.println(url);
        // }
    }

    public SpiderRule getOSCRule() {
        SpiderRule rule = new SpiderRule();
        rule.setListRule("#RecentNewsList ul.List li h2");
        rule.setTitleRule(".NewsEntity .OSCTitle");
        rule.setContentRule(".NewsEntity .NewsContent");
        return rule;
    }
}
